package org.icress.fusion.b2b

class DealerController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 100, 100)
        render view : '/b2b/dealer/list', model : [dealerInstanceList: Dealer.list(params), dealerInstanceTotal: Dealer.count()]
    }

    def create = {
        def dealerInstance = new Dealer()
        dealerInstance.properties = params
        render view : '/b2b/dealer/create', model : [dealerInstance: dealerInstance]
    }

    def save = {
        def dealerInstance = new Dealer(params)
        dealerInstance.validate()
        if (!dealerInstance.hasErrors() && dealerInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), dealerInstance.id])}"
            redirect(action: "show", id: dealerInstance.id)
        }else{
            render(view: "/b2b/dealer/create", model: [dealerInstance: dealerInstance])
        }
    }

    def show = {
        def dealerInstance = Dealer.get(params.id)
        if (!dealerInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/b2b/dealer/show', model : [dealerInstance: dealerInstance]
        }
    }

    def edit = {
        def dealerInstance = Dealer.get(params.id)
        if (!dealerInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/b2b/dealer/edit', model : [dealerInstance: dealerInstance]
        }
    }
    
    def update = {
        def dealerInstance = Dealer.get(params.id)
        if (dealerInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (dealerInstance.version > version) {
                    
                    dealerInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'b2b.dealer.label', default: 'Dealer')] as Object[], "Another user has updated this Dealer while you were editing")
                    render(view: "/b2b/dealer/edit", model: [dealerInstance: dealerInstance])
                    return
                }
            }
            dealerInstance.properties = params
            dealerInstance.validate()
            if (!dealerInstance.hasErrors() && dealerInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), dealerInstance.id])}"
                redirect(action: "show", id: dealerInstance.id)
            }else {
                render(view: "/b2b/dealer/edit", model: [dealerInstance: dealerInstance])
            }
        } else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def dealerInstance = Dealer.get(params.id)
        if (dealerInstance) {
            try {
                dealerInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.dealer.label', default: 'Dealer'), params.id])}"
            redirect(action: "list")
        }
    }
}
